<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
        <el-select v-model="query.scope" filterable clearable placeholder="作用域" class="filter-item" @change="crud.toQuery">
          <el-option
            v-for="item in my_scope"
            :key="item.value"
            :label="item.name"
            :value="item.value"
          />
        </el-select>
        <el-select v-model="query.projectId" filterable clearable placeholder="服务名称" class="filter-item" @change="crud.toQuery">
          <el-option
            v-for="item in serverList"
            :key="item.projectId"
            :label="item.name"
            :value="item.projectId"
          />
        </el-select>
        <el-input v-model="query.name" clearable placeholder="变量名称" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <rrOperation :crud="crud" />
      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission" />
      <!--表单组件-->
      <el-dialog :close-on-click-modal="false" :before-close="crud.cancelCU" :visible.sync="crud.status.cu > 0" :title="crud.status.title" width="500px">
        <el-form ref="form" :model="form" :rules="rules" size="small" label-width="80px">
          <el-form-item label="变量名称" prop="name">
            <el-input v-model="form.name" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="变量的值" prop="value">
            <el-input v-model="form.value" :rows="8" type="textarea" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="作用域" prop="scope">
            <el-select v-model="form.scope" style="width: 370px;" filterable placeholder="请选择">
              <el-option
                v-for="item in my_scope"
                :key="item.value"
                :label="item.name"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="服务名称" prop="projectId">
            <el-select v-model="form.projectId" style="width: 370px;" filterable clearable placeholder="请选择">
              <el-option
                v-for="item in serverList"
                :key="item.projectId"
                :label="item.name"
                :value="item.projectId"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="变量说明" prop="value">
            <el-input v-model="form.description" :rows="2" type="textarea" style="width: 370px;" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.status.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table ref="table" v-loading="crud.loading" :data="crud.data" size="small" style="width: 100%;" @selection-change="crud.selectionChangeHandler">
        <el-table-column type="selection" width="55" />
        <el-table-column prop="scope" label="作用域" width="100">
          <template slot-scope="scope2">
            {{ my_scope[scope2.row.scope].name }}
          </template>
        </el-table-column>
        <el-table-column prop="projectName" label="服务名称" width="255" />
        <el-table-column prop="name" label="变量名称" width="155" />
        <el-table-column prop="value" label="变量的值" />
        <el-table-column prop="description" label="说明" width="155" />
        <el-table-column v-if="checkPer(['admin','dpEnvVariables:edit','dpEnvVariables:del'])" label="操作" width="250" align="center">
          <template slot-scope="scope">
            <udOperation
              :data="scope.row"
              :permission="permission"
            />
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import crudDpEnvVariables from '@/api/deploy/dpEnvVariables'
import { getAllServerList } from '@/api/deploy/dpServerConfig'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'
import { mapGetters } from 'vuex'

const defaultForm = { id: null, env: null, scope: null, projectId: null, name: null, value: null, description: null }
export default {
  name: 'DpEnvVariables',
  components: { pagination, crudOperation, rrOperation, udOperation },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  cruds() {
    return CRUD({ title: '环境变量配置', url: 'api/dpEnvVariables', idField: 'id', sort: 'id,desc', crudMethod: { ...crudDpEnvVariables }})
  },
  data() {
    return {
      my_scope: [{ 'name': '全局', 'value': 0 }, { 'name': '服务', 'value': 1 }],
      serverList: [],
      permission: {
        add: ['admin', 'dpEnvVariables:add'],
        edit: ['admin', 'dpEnvVariables:edit'],
        del: ['admin', 'dpEnvVariables:del']
      },
      rules: {
        env: [
          { required: true, message: '环境不能为空', trigger: 'blur' }
        ],
        projectId: [
          { validator: this.checkServer, message: '服务不能为空', trigger: 'blur' }
        ],
        scope: [
          { required: true, message: '0-全局 1-服务', trigger: 'blur' }
        ],
        name: [
          { required: true, message: '变量名称不能为空', trigger: 'blur' }
        ],
        value: [
          { required: true, message: '变量的值不能为空', trigger: 'blur' }
        ]
      },
      queryTypeOptions: [
        { key: 'env', display_name: '环境' },
        { key: 'scope', display_name: '0-全局 1-某个服务' },
        { key: 'projectId', display_name: '某服务名称，当scope=1时，这个字段有值' },
        { key: 'name', display_name: '变量名称' }
      ]
    }
  },
  computed: {
    ...mapGetters([
      'env'
    ])
  },
  created() {
    this.$nextTick(() => {
      getAllServerList().then(data => {
        this.serverList = data
      })
    })
    // 监听应用下拉框选择事件
    this.$bus.$on('global-env-change', this.crud.toQuery)
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      this.query.env = this.env
      return true
    },
    [CRUD.HOOK.beforeToAdd]() {
      this.form.env = this.env
      return true
    },
    [CRUD.HOOK.beforeToEdit]() {
      console.log(this.serverList)
      this.form.projectId = `${this.form.projectId}`
    },
    checkServer(rule, value, callback) {
      if (this.form.scope === 1 && (value === null || String(value).trim() === '')) {
        callback(new Error('服务不能为空!'))
      } else {
        callback()
      }
    }
  }
}
</script>

<style scoped>
</style>
